package cfca.paperless.client.servlet._2_extend._2_07_SynthesizeOuterSignatureAndSealPdf;

import java.util.Date;

import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;
import cfca.sadk.util.Base64;

public class SynthesizeOuterSignatureAndSealPdfTest01 {

    /**
     * PDF合成外部签名并签章接口
     * @param args
     * @throws IOException
     * @throws
     */
    public static void main(String[] args) throws Exception {
        try {
            long timeStart = System.currentTimeMillis();// 开始时间
            // 初始化客户端对象
            PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.extUrl, 10000, 60000);// 无纸化辅助接口的访问URL

            // 操作员编码或者机构号
            String operatorCode = PaperlessConfig.operatorCode;
            // 渠道编码，可以为空
            String channelCode = PaperlessConfig.channelCode;

            String sigString = "MIIE8AYJKoZIhvcNAQcCoIIE4TCCBN0CAQExCzAJBgUrDgMCGgUAMAsGCSqGSIb3DQEHAaCCA7EwggOtMIIClaADAgECAgUQAnZCAzANBgkqhkiG9w0BAQUFADBYMQswCQYDVQQGEwJDTjEwMC4GA1UEChMnQ2hpbmEgRmluYW5jaWFsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MRcwFQYDVQQDEw5DRkNBIFRFU1QgT0NBMTAeFw0xNjA0MTEwNDMyMTJaFw0xOTA1MTEwNDMyMTJaMHAxCzAJBgNVBAYTAkNOMRcwFQYDVQQKEw5DRkNBIFRFU1QgT0NBMTERMA8GA1UECxMITG9jYWwgUkExFTATBgNVBAsTDEluZGl2aWR1YWwtMTEeMBwGA1UEAxQVMDUxQGFzQFoxMjM0NTY3ODkwQDQ4MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQsPuAOT8krt5OLz9UqFDQpuKZ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1PEdBYitQjKEIY98aS2JRZ1yIO1EOI6U/g4SfWF38z4FUx4iIgxT2aTAV0UsqwaqYwlxEwIUrxim3HtnuBRYamedWBfPKzeP8S4H2iIaPo1dmHX3QQNcMRWdYYg=";
            byte[] signature = Base64.decode(sigString);
            String id = "201803211049535843918843429971";
            String bizSerialNo = GUID.generateId();
//            PdfBean pdfBean = new PdfBean("", bizSerialNo, "B001", "auto");
            String bizTypeCode = "001";
            String outputFilePath = "cfca1234";
            PdfBean pdfBean = new PdfBean("", bizSerialNo, bizTypeCode, outputFilePath, "");
            pdfBean.setPdfId(id);
            String pdfBeanXml = PaperlessUtil.buildPdfBeanXml(pdfBean);
            String sealStrategyListXml = createSealStrategyListXml();
            
            // 获取时间戳的方式。默认值为0。0：实时访问CFCA 时间戳服务；1：使用从CFCA购置并在本地部署的时间戳服务器产品；
            String timestampChannel = "0";
            // 合成签名并签章
            byte[] sealedPdfData = clientBean.synthesizeOuterSignatureAndSealPdf(signature, pdfBeanXml, sealStrategyListXml, operatorCode, channelCode, timestampChannel);
            String errorRsString = PaperlessUtil.isError(sealedPdfData);

            if ("".equals(errorRsString)) {
                System.out.println("OK:synthesizeOuterSignatureAndSealPdf");
                System.out.println(sealedPdfData.length);
                IoUtil.write("./TestData/output/sealedPdf-synthesizeOuterSignatureAndSealPdf.pdf", sealedPdfData);
            } else {
                // 处理结果为异常，打印出错误码和错误信息
                System.out.println("NG:synthesizeOuterSignatureAndSealPdf");
                System.out.println(sealedPdfData.length);
                System.out.println(new String(sealedPdfData, "UTF-8"));
            }
            long timeEnd = System.currentTimeMillis();// 结束时间
            System.out.println("time:" + (timeEnd - timeStart));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static String createSealStrategyListXml() throws Exception {
        String sealCode = "yz201803198562114";
        String sealPassword = "";
        
        SealUserInfo sealUserInfo = new SealUserInfo("张三", "beijing", "reason");//sealUserInfo.setVisible(false);
        // 3=关键字签章
        SignLocation signLocation = new SignLocation(1, 400, 400);
        
        // 生成签章策略文件
        String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, sealUserInfo, signLocation);
        
        return "<List>" + sealStrategyXml + "</List>";
    }

}
